package com.system.commonutils.utils;

import com.system.commonutils.bean.AliResponseEntity;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 阿里云短信发送
 */
public class SendSmsUtils {
    public static String SignName="信安OA";

    public static AliResponseEntity sendSms(String phone, String code){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",code);
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                "LTAI4FhqufSLF4WvSNEnueJR",
                "3l2upiPOFYON2Bhh0LA0AuOKuvuNoi");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", "SMS_186618380");
        request.putQueryParameter("TemplateParam", jsonObject.toString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            AliResponseEntity aliResponseEntity = JSONObject.parseObject(data, AliResponseEntity.class);
            return aliResponseEntity;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
